package Learning.calculator;

/**
 * Author: $Author: mauriciosantoshoyos $
 * Date Created: 8 June 2010
 * Date Modified: $Date: 2010-06-18 06:16:51 +0000 (Fri, 18 Jun 2010) $
 * File: $HeadURL: http://droidsandbox.googlecode.com/svn/trunk/Learning/Mauricios_LearningProject/Calculator/src/Learning/calculator/Calculator.java $
 * Version: $Rev: 69 $
 *
 * Simple calculator to test input/output..
 */


import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Scroller;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class Calculator extends Activity {
    /** Called when the activity is first created. */
    View.OnClickListener listener;
    
    LinearLayout layout;
    TableLayout table;
    TableRow row;
    
    LinearLayout.LayoutParams linearLO;
    TableLayout.LayoutParams tableLO;
    TableRow.LayoutParams rowLO;
    
    TextView display;
    Button btn;
    
    
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        linearLO = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        
        Scroller scroll = new Scroller(this);
        
        layout = new LinearLayout(this);
        layout.setOrientation(1);
        table = new TableLayout(this);
        table.setStretchAllColumns(true);
        setContentView(layout);
        
        display = new TextView(this);
        display.setScroller(scroll);
        display.setHorizontallyScrolling(true);
        //display.setSingleLine(true);
        display.setCursorVisible(true);
        display.setTextSize(30);
        display.setGravity(Gravity.RIGHT);

        layout.addView(display,linearLO);
        layout.addView(table);       
        
        //Create content
        buildInterface();

    }

    public void buildInterface(){   
        //Display layout        
        tableLO = new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,
                TableLayout.LayoutParams.WRAP_CONTENT);
        
        rowLO = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
                TableRow.LayoutParams.WRAP_CONTENT);
        
        String values[] = {"7","8","9","/","4","5","6","x","1","2","3","-",".","0","=","+"};
        int counter = 0;
        
        //Rows
        for(int i=0;i<4;i++) {
            row = new TableRow(this);
            //Columns
            for(int j=0;j<4;j++){
                //Create button
                btn = new Button(this);
                btn.setTextSize(30);
                btn.setContentDescription(values[counter]);
                btn.setText(values[counter++]);
                btn.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                        display.append(v.getContentDescription()); 
                    }
                });
                row.addView(btn,rowLO);             
            }//End Columns
            table.addView(row,tableLO);
        }//End Rows
        
        //Add 
    }
}






